#include <iostream>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <cstring>
using namespace std;

struct st_pid
{
    int pid;       // 进程编号
    char name[51]; // 进程名称
};
int main(int argc, char *argv[])
{
    int shmid = shmget(0x5005, sizeof(st_pid), 0640 | IPC_CREAT);
    if (shmid == -1)
    {
        printf("shmget(0x5005) failed\n");
        return -1;
    }
    struct st_pid *stpid;
    stpid = (struct st_pid *)shmat(shmid, 0, 0);
    if (stpid == (void *)-1)
    {
        printf("shmat(0x5005) failed\n");
        return -1;
    }
    // 操作共享内存

    printf("pid===%d  name==%s\n", stpid->pid, stpid->name);
    stpid->pid = getpid();
    strcpy(stpid->name, argv[1]);
    printf("pid===%d  name==%s\n", stpid->pid, stpid->name);

    //把共享内存从当前进程分离
    shmdt(stpid);

    //删除共享内存 
    // int ret=shmctl(shmid,IPC_RMID,0);
    // if(ret==-1){
    //     printf("shmctl failed\n");
    //     return -1;
    // }
    return 0;
}
